package   engine.display.newAttack.action
{
	import engine.display.newAttack.AttackLogic;
	import engine.display.newAttack.AttackPet;
	import engine.display.newAttack.AttackPlayer;
	import engine.display.newAttack.action.effect.PointPlayLite;
	import engine.display.newAttack.vo.AttackActionVo;
	import engine.display.newAttack.vo.DamageVo;
	import engine.display.newAttack.vo.FlowVo;
	import engine.flag.PlayerActionFlag;
	import engine.logic.McPool;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.utils.getTimer;
	import flash.utils.setTimeout;
	
	import newx.StageInfo;
	
	public class AreaAttack extends AttackAction
	{
//		/**数据模型*/
//		private var model				:ModelLocator;
		/**施法者*/
		private var _from				:AttackPlayer;
		private var _tPlayers			:Array;
		
		private var _damageVos			:Vector.<DamageVo>;
		
		private var _flowVo			:FlowVo
		
		private var _attackActionVo	:AttackActionVo;
		
		/**施法容器*/
		private var _effectSpace		:DisplayObjectContainer;
				
		/**特效坐标*/
		private var _startPoint		:Point;
		
		/**特效资源*/
		private var _skin				:MovieClip;
		
		/**是否已经击中*/
		private var _isAttack			:Boolean	=	false;
		
		private var _soundHit			:String;
		
		/**播放完成后播放*/
		protected var _onPlayOver		:Function;
		
		/**
		 *	连击 
		 */		
		private var _doubleHit			:int;
		
		private var _playStatus		:int;
		
		public function AreaAttack(		
			fPlayer				:AttackPlayer,
			tPlayers			:Array,
			flowVo				:FlowVo,
			attackActionVo		:AttackActionVo,
			effectSpace			:Sprite,
			onPlayOver			:Function
			)
		{
			_from				=	fPlayer;
			_tPlayers			=	tPlayers;
			_tPlayers.shift();
			_flowVo				=	flowVo;
			_damageVos			=	_flowVo.damageVos;
			_attackActionVo		=	attackActionVo;
			_doubleHit			=	_flowVo.doubleHit;
			_effectSpace		=	effectSpace;
			
			_startPoint			=	attackActionVo.startPoint;
			
			_skin				=	McPool.borrowRes( attackActionVo.effectSkin ) as MovieClip;
			
			_onPlayOver			=	onPlayOver;
			
//			model	=	ModelLocator.getInstance();
		}
		
		/**
		 * 
		 * @param pActID ：施法动作
		 * 
		 */		
		public function play(val:int,frameLabel:String):void
		{
			if( _attackActionVo.needSceneBlack )
			{
				//黑场景
				playBlackEffect();
//				setTimeout(playStartEffect,
//					_attackActionVo.startEffecDelay - BLACKSCENE_EFFECT_DELAY,
//					val,
//					frameLabel,
//					BLACKSCENE_EFFECT_DELAY);
			}
//			else
//			{
				playStartEffect(val,frameLabel);
//			}
		}
		
		/**
		 * 播放开始特效
		 * */
		private function playStartEffect(
			val			:int,
			frameLabel	:String,
			delay		:int = 0):void
		{
			//播放释放技能前的特效，如果有
//			if( _attackActionVo.startEffectData != null )
//			{
//				new PointPlayLite(_from.effectPlace,
//					_from[ _attackActionVo.beAttactEffectPos+ "Point" ],
//					McPool.borrowRes(_attackActionVo.startEffectData["skin"]),
//					_attackActionVo.startEffectData);
				
//				setTimeout( startMainEffect, _attackActionVo.startEffecDelay - delay, val, frameLabel );
//			}
//			else
//			{
				startMainEffect(val,frameLabel);
//			}
		}
		
		/**
		 * 开始播放施法动作，并尝试产生特效 
		 * @param val
		 * @param frameLabel
		 * 
		 */		
		public function startMainEffect(val:int,frameLabel:String):void		
		{
			_doubleHit--;
			
			//生成特效
			_from.playToTarget(val);
			
			_from.addEventListener(Event.ENTER_FRAME,onPlayAttack);
		}
		
		protected function onPlayAttack(e:Event):void
		{
//			if(_from is AttackPet)
//			{
////				_from.getSkin().gotoAndPlay(PlayerActionFlag.Delight);
//				return;
//			}
			
			if( 	_from.getSkin().currentLabel	==	 PlayerActionFlag.MagicAttack 
				||  _from.getSkin().currentLabel 	==	 PlayerActionFlag.NormalAttack
				||  _from.getSkin().currentLabel	==	 PlayerActionFlag.Delight)
			{
				_from.removeEventListener(Event.ENTER_FRAME,onPlayAttack);
				
				createEffect();
			}
		}
		
		private function createEffect():void
		{
			var	tScaleX:int;
			
			if(_skin == null)
			{
				playBeAttackEffect();
				onEffectEnd();
				return;
			}
			
			if(_from.team == 0)
			{
				_skin.scaleX	=	1;
			}
			else
			{
				_skin.scaleX	=	-1;
			}
			
			if( _attackActionVo.startPoint != null )
			{//说明是打行或者列攻击
				new PointPlayLite( _effectSpace,
					_startPoint,
					_skin,
					null,
					onEffectPlay,onEffectEnd);
				
			}else
			{
				new PointPlayLite( _tPlayers[ 0 ].effectPlace,
					_tPlayers[ 0 ][  _attackActionVo.effectPos+ "Point" ],
					_skin,
					null,
					onEffectPlay,onEffectEnd);
			}
		}
		
		private function playBeAttackEffect():void
		{
			_isAttack	=	true;
			
			var tPlayer	:AttackPlayer;
			
			if( 0 >= _doubleHit )
			{
				for( var i : int = 0; i < _tPlayers.length; i++ )
				{
					beAttack( _tPlayers[ i ], _damageVos[ i ] );
				}
			}else
			{
				beAttack( _tPlayers.shift(), _damageVos.shift() );
			}
			
			
			if( isCrit() )
			{
				shake( AttackLogic.getInstance().attackScene );
			}
		}
		
		private function beAttack( player : AttackPlayer, damage : DamageVo ):void
		{
			var tPlayer : AttackPlayer = player;
			
			tPlayer.damage( damage );
			
			if( damage.result != 5 && _attackActionVo.beAttactEffectSkin != null )
			{
				var mc : MovieClip = McPool.borrowRes( _attackActionVo.beAttactEffectSkin )
				
				if(_from.team == 0)
				{
					mc.scaleX	=	1;
				}
				else
				{
					mc.scaleX	=	-1;
				}
				new PointPlayLite(tPlayer.effectPlace,//_attackActionVo.beAttactEffectData["skin"]
					tPlayer[ _attackActionVo.beAttactEffectPos+ "Point" ],
					mc );
			}
			if(!(_from is AttackPet))
			{
				_from.showBuffer( _flowVo.buffer );
				_from.resetMp( _flowVo.mp );
				_from.resetHp( _from.fightInfoVo.hp - _flowVo.bufferhp, _from.fightInfoVo.maxHp - _flowVo.buffermp );
				
				_from.buffHp( _flowVo.bufferhp );
				_from.buffMp( _flowVo.buffermp );
			}
		}
		
		private function onEffectPlay(val:PointPlayLite):void
		{
			if(!_isAttack && ( val.effectMovie.currentLabel == "击中针" || val.effectMovie.currentFrame == val.effectMovie.totalFrames ) )
			{
				playBeAttackEffect();
//				ModelLocator.getInstance().dispatchEvent(new SoundEvent(_soundHit));
			}//
		}
		
		private function onEffectEnd( p : * = null ):void
		{
			if( 0 < _doubleHit )
			{//去连击
				_isAttack = false;
				startMainEffect( _attackActionVo.skillAttackType,"Attack" );
				return;
			}
			
			if( _attackActionVo.needSceneBlack )
			{
				try
				{
					var _skin		:MovieClip	=	McPool.borrowRes( 
						_attackActionVo.beAttactEffectSkin);
					//亮场景
					setTimeout(removeBlackEffect,
						_skin.movie.totalFrames*1000/StageInfo.stage.frameRate);
				}
				catch(e:Error)
				{
					//亮场景
					removeBlackEffect();
				}
			}
			
			setTimeout( _onPlayOver, 500 );
		}
		
		protected function isShake():Boolean
		{
			return _attackActionVo.skillAttackType == 5 && isCrit();
		}
		
		protected function isCrit():Boolean
		{
			for each( var damageVo : DamageVo in _damageVos )
			{
				if( damageVo.result == 2 )
				{
					return true;
				}
			}
			return false
		}
	}
}